package com.smart.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.smart.domain.StudentUser;
/*数据库连接类  该类中包含一个从数据库的stu表查询学生信息的方法和一个获得本实例的静态方法*/
public class DBselect {
    public List find() {
        List<StudentUser> list = new ArrayList<StudentUser>();
        final String DRIVER = "com.mysql.jdbc.Driver";
        final String USER = "root";
        final String PWD = "147852369";
        final String URL = "jdbc:mysql://localhost:3306/student";// 建立数据库连接
        Connection connection = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USER, PWD);
            String sql = "select * from stu";
            pstmt = connection.prepareStatement(sql); // 创建用户操作执行SQL语句的PreparedStatement对象
            rs = pstmt.executeQuery();
            while (rs.next()) {
                StudentUser s1 = new StudentUser();
                s1.setName(rs.getString("name"));
                s1.setId(rs.getString("id"));
                s1.setSclass(rs.getString("sclass"));
                s1.setGrade(rs.getString("grade"));
                s1.setSex(rs.getString("sex"));
                list.add(s1);
            }
            pstmt.executeUpdate(); // 编译执行insert语句

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return list;

    }
}